﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.WindowsAzure.StorageClient;

namespace Ogdi.Data.DataLoader
{
    public class TableMetadataEntity
    {
        //constructors
        public TableMetadataEntity() { }
        public TableMetadataEntity(TableEntity data) 
        {
            foreach (var prop in data.GetProperties())
            {
                if (prop.Name.ToLower() == "name")
                    Name = prop.Value.ToString();
                if (prop.Name.ToLower() == "category")
                    Category = prop.Value.ToString();
                if (prop.Name.ToLower() == "description")
                    Description = prop.Value.ToString();
                if (prop.Name.ToLower() == "source")
                    Source = prop.Value.ToString();
                if (prop.Name.ToLower() == "metadataurl")
                    MetadataUrl = prop.Value.ToString();
                if (prop.Name.ToLower() == "entityset")
                    EntitySet = prop.Value.ToString();
                if (prop.Name.ToLower() == "entitykind")
                    EntityKind = prop.Value.ToString();
                if (prop.Name.ToLower() == "lastupdatedate")
                {
                    LastUpdateDate = DateTime.Parse(prop.Value.ToString());
                }
            }
        }

        public string Name { get; set; }
        
        public string Category { get; set; }
        
        public string Description { get; set; }
        
        public string Source { get; set; }
        
        public DateTime LastUpdateDate { get; set; }
        
        public string MetadataUrl { get; set; }
        
        public string EntitySet { get; set; }
        
        public string EntityKind { get; set; }

        public static implicit operator Entity(TableMetadataEntity me)
        {
            Entity e = new Entity();
            foreach (var pi in me.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                e.AddProperty(pi.Name, pi.GetValue(me, null));
            }
            return e;
        }
    }
}
